package org.hxyjs.dao.impl;

import org.hxyjs.dao.ShoppingtrolleyDao;
import org.hxyjs.entity.Shoppingtrolley;
import org.hxyjs.mapper.ShoppingtrolleyMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("shoppingtrolleyDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ShoppingtrolleyDaoImpl implements ShoppingtrolleyDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ShoppingtrolleyMapper shoppingtrolleyMapper;
    public List<Shoppingtrolley>  getAllShoppingtrolleys() throws Exception{
        return shoppingtrolleyMapper.selectByExample(null);
    }

    @Override
    public Integer deleteShoppingtrolleyByStno(String stno)  throws Exception{
        return shoppingtrolleyMapper.deleteByPrimaryKey(stno);
    }

    @Override
    public Integer updateShoppingtrolleyByStno(Shoppingtrolley shoppingtrolley)  throws Exception{
        return shoppingtrolleyMapper.updateByPrimaryKey(shoppingtrolley);
    }

    @Override
    public Integer addShoppingtrolley(Shoppingtrolley shoppingtrolley)  throws Exception{
        shoppingtrolley.setStno(initId());
        return shoppingtrolleyMapper.insertSelective(shoppingtrolley);
    }
    public String initId(){
        String maxStno = shoppingtrolleyMapper.getMaxStno();
        return GenNumTools.initId("",3,maxStno);
    }
    @Override
    public List<Shoppingtrolley> getShoppingtrolleyByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)shoppingtrolleyMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return shoppingtrolleyMapper.getShoppingtrolleyByPage(map);
    }
}
